package com.warrantyhotel.android.adapter;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Account_Upload_Transfers_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Free_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Information_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_RSS_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_Transactions_Table;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.Inbox_WarrantyTransfer_Table;

public class InboxAdapter extends CursorAdapter {

	private LayoutInflater mInflater;

	public InboxAdapter(Context context, Cursor c) {
		super(context, c, false);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final RssItemViewHolder listItem = (RssItemViewHolder) view.getTag();
		final String InfoTitle = cursor.getString(cursor.getColumnIndex(Inbox_Information_Table.TITLE));
		final String description = cursor.getString(cursor.getColumnIndex(Inbox_Information_Table.TEXT));
		final String name_table = cursor.getString(cursor.getColumnIndex(Inbox_Information_Table.NAME_TABLE));

		int drawable_id = 0;

		if (name_table.equals(Inbox_Free_Table.TABLE_NAME))
			drawable_id = R.drawable.icn_info;
		else if (name_table.equals(Inbox_Information_Table.TABLE_NAME))
			drawable_id = R.drawable.icn_info;
		else if (name_table.equals(Inbox_RSS_Table.TABLE_NAME))
			drawable_id = R.drawable.icn_mail;
		else if (name_table.equals(Inbox_Transactions_Table.TABLE_NAME))
			drawable_id = R.drawable.icn_new;
		else if (name_table.equals(Inbox_WarrantyTransfer_Table.TABLE_NAME))
			drawable_id = R.drawable.icn_mail;
		else if (name_table.equals(Inbox_Account_Upload_Transfers_Table.TABLE_NAME))
			drawable_id = R.drawable.icn_mail;

		listItem.FreeImage.setImageDrawable(context.getResources().getDrawable(drawable_id));
		listItem.txtInfoTitle.setText(InfoTitle);

		if (null != description) {
			listItem.txtInfoDescription.setText(description);
		}
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = mInflater.inflate(R.layout.inbox_item, null);
		final RssItemViewHolder listItem = new RssItemViewHolder();
		listItem.txtInfoTitle = (TextView) view.findViewById(R.id.inbox_title_item);
		listItem.txtInfoDescription = (TextView) view.findViewById(R.id.inbox_description_item);
		listItem.FreeImage = (ImageView) view.findViewById(R.id.inbox_thumbnail_item);
		view.setTag(listItem);
		return view;
	}

	public class RssItemViewHolder {
		TextView txtInfoTitle;
		TextView txtInfoDescription;
		ImageView FreeImage;
	}

}
